home *** CD-ROM | disk | FTP | other *** search
/ MacPeople 2003 February 1 / MACPEOPLE-2003-02-01.ISO.7z / MACPEOPLE-2003-02-01.ISO / ぶらりオンラインウェアの旅 / おしゃべり漂流記 / xGates / xGates 1.2 Source Code.sit / xGates 1.2 Source Code / util.c < prev    next >
Text File  |  2002-12-08  |  6KB  |  186 lines

  1. /*
  2.     xGates -- Stunningly entertaining action game for MacOS Classic / MacOS X
  3.     Copyright (C) 2002 Sveinbjorn Thordarson <paladeen@soth.zoneit.com>
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     util.c - a collection of generally useful functions
  20.  
  21. */
  22.  
  23. #include "externs.h"
  24. #include "prototypes.h"
  25. #include "definitions.h"
  26.  
  27.  
  28. //////////////////////////////////////////////////////
  29. //fast, efficient function to copy pascal strings
  30. //courtesy of Michael Gleason
  31. //mgleason@cse.unl.edu
  32. //////////////////////////////////////////////////////
  33.  
  34. unsigned char *pStrcpy(register unsigned char *dst, register unsigned char *src)
  35. {
  36.     #define word short
  37.     #define IsWordAligned(a)  (((word) (a) & (word)(sizeof(word) - 1)) == 0)
  38.  
  39.  
  40.     register short          nLWords;
  41.     register short          len = src[0] + 1;
  42.     unsigned char           *s = dst;
  43.  
  44.     if (IsWordAligned(dst) && IsWordAligned(src)) 
  45.     {
  46.  
  47.         nLWords = len / (short)sizeof(long);
  48.         
  49.         /* This is the number of single bytes to copy afterward,
  50.          * which is len (mod 4).  We can do powers of 2 with &. */
  51.         len &= ((short)(sizeof(long) - 1));
  52.         
  53.         for ( ; nLWords > 0; --nLWords) {
  54.             /* I'd like to be able to do this as an autoincrement
  55.              * statement, but C won't let me.  I want to stay
  56.              * away from 68000 gazzembly now that PowerPC is here,
  57.              * otherwise I'd just stick in an asm { move.l (src)+, (dst)+ }
  58.              * here.  Actually I timed that too, and it's slower!
  59.              */
  60.             *(long *)dst = *(long *)src;
  61.             dst += sizeof(long);
  62.             src += sizeof(long);
  63.         }
  64.         for (; len > 0; --len ) {
  65.             *dst++ = *src++;
  66.         }
  67.     } else for (; --len >= 0; ) {
  68.         /* Just copy it the usual way, byte by byte. In the 68000's
  69.          * case, it's unlikely we'll ever reach this point since
  70.          * p-strings are almost always word-aligned anyway.
  71.          */
  72.         *dst++ = *src++;
  73.     }
  74.     
  75.     return(s);
  76. }   /* pStrcpy */
  77.  
  78.  
  79. //////////////////////////////////////////////////////
  80. //smarter than events.  Checks keymap to see if a 
  81. //designated key is down
  82. //courtesy of Thorlindur Thorolfsson, =)
  83. //////////////////////////////////////////////////////
  84.  
  85. Boolean CheckKeyMapForKey(KeyMap theKeyMap, short theKey)
  86. {
  87.     short    byteIndex;
  88.     char    theByte, theBit;
  89.     char    *thePointer;
  90.     
  91.     byteIndex = theKey >> 3;
  92.     thePointer = (char *)&theKeyMap[0];
  93.     theByte = *(char *)(thePointer + byteIndex);
  94.     theBit = 1L<<(theKey & 7);
  95.     return( (theByte & theBit) != 0 );
  96. }
  97.  
  98. //////////////////////////////////////////////////////
  99. //get a random between two numbers
  100. //////////////////////////////////////////////////////
  101.  
  102. short RandomBetween(short value1, short value2)
  103. {
  104.     short temp;
  105.     
  106.     temp = value2 - value1 + 1;
  107.     
  108.     if(temp == 0)/*Justin Case*/
  109.         temp = 1;
  110.     
  111.     temp = (Random() & 0x7fff) % temp;
  112.     
  113.     return temp + value1;
  114. }
  115.  
  116. //////////////////////////////////////////////////////
  117. //get absolute value of number
  118. //////////////////////////////////////////////////////
  119. short absolute (short value)
  120. {
  121.     if (value < 0)
  122.     {
  123.         return(value * -1);
  124.     }
  125.  
  126.     return(value);
  127. }   
  128.  
  129. //////////////////////////////////////////////////////
  130. //use Quickdraw pen modes to darken a designated area
  131. //////////////////////////////////////////////////////
  132. void ShadeRect (Rect *rect)
  133. {
  134.     RGBForeColor(&myGrayColor);
  135.     PenMode(subPin);
  136.     PaintRect(rect);
  137.     ForeColor(blackColor);
  138.     PenNormal();
  139.     RGBForeColor(&myBlackColor);
  140. }
  141.  
  142. //////////////////////////////////////////////////////
  143. //do nothing for a designated number of ticks
  144. //////////////////////////////////////////////////////
  145. void Sleep (short ticks)
  146. {
  147.     long count = TickCount();
  148.  
  149.     while (TickCount() < count+(ticks)) {    }
  150.  
  151. }
  152.  
  153. //////////////////////////////////////////////////////
  154. //place a rect in the center of another rect
  155. //////////////////////////////////////////////////////
  156. Rect* CenterRectInRect(Rect *smallRect, Rect *bigRect)
  157. {
  158.     Rect newRect;
  159.     
  160.     newRect.left = (bigRect->right - smallRect->right) / 2;
  161.     newRect.right = ((bigRect->right - smallRect->right) / 2) + smallRect->right;
  162.     
  163.     newRect.top = (bigRect->bottom - smallRect->bottom) / 2;
  164.     newRect.bottom = ((bigRect->bottom - smallRect->bottom) / 2) + smallRect->bottom;
  165.     
  166.     return (&newRect);
  167. }
  168.  
  169. //////////////////////////////////////////////////////
  170. //Red Alert dialog
  171. //////////////////////////////////////////////////////
  172. void DoAlert(Str255 errorString, Str255 expStr)
  173. {
  174.     StandardAlert (kAlertStopAlert, errorString,  expStr, NULL, NULL);
  175. }
  176.  
  177. //////////////////////////////////////////////////////
  178. //Red Alert dialog and then quit app
  179. //////////////////////////////////////////////////////
  180. void FatalErrorAlert(Str255 errorString, Str255 expStr)
  181. {
  182.     StandardAlert (kAlertStopAlert, errorString,  expStr, NULL, NULL);
  183.     QuitApp();
  184. }
  185.  
  186.